{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'docs':[\n",
    "        'my dog has flea promble help please',\n",
    "        'maybe not take him to dog park stupid',\n",
    "        'my dalmation is so cute I love him',\n",
    "        'stop posting stupid worthless garbage',\n",
    "        'mr licks ate my steak how to stop him',\n",
    "        'quit buying worthless dog food stupid',\n",
    "    ],\n",
    "    'labels':[0,1,0,1,0,1]\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['my', 'dog', 'has', 'flea', 'promble', 'help', 'please']"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'my dog has flea promble help please'.split(' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "data['docs']=list(map(lambda doc:doc.split(' '),data['docs']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getDocList(docs):\n",
    "    docSet = set([])\n",
    "    for doc in docs:\n",
    "        docSet = set(doc) | docSet\n",
    "    doclist = list(docSet)\n",
    "    doclist.sort()\n",
    "    return doclist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "docList = getDocList(data['docs'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "def doc2V(doc,docList):\n",
    "    dims = len(docList)\n",
    "    doc_v = [0]*dims\n",
    "    for word in doc:\n",
    "        if word in doc:\n",
    "            doc_v[docList.index(word)] +=1\n",
    "    return doc_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "print(doc2V(data['docs'][0],docList))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fromDoc_v(docList):\n",
    "    docs_v = [] \n",
    "    for doc in data['docs']:\n",
    "        docs_v.append(doc2V(doc(doc,docList)))   \n",
    "    return docs_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [],
   "source": [
    "docs_v = list(map(lambda doc:doc2V(doc,docList),data['docs']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [],
   "source": [
    "#P(A|B) * P(B) = P(B|A) * P(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "#P(doc|class=1)\n",
    "#P(doc|class=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [],
   "source": [
    "#P(C1|doc) * P(doc) = P(doc|C1) * P(C1)\n",
    "#P(C0|doc) * P(doc) = P(doc|C0) * P(C0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''函数功能：\n",
    "贝叶斯训练\n",
    "输入：\n",
    "docs_v:doc的向量矩阵labels:标签list docList（相量空间的基）\n",
    "输出：\n",
    "'''\n",
    "\n",
    "def train(docs_v,labels,docList):\n",
    "    n_doc = len(labels)\n",
    "    docLen = len(docList)\n",
    "    #防止0概率出现\n",
    "    p1num = np.ones(docLen)\n",
    "    p0num = np.ones(docLen)\n",
    "    p1Denom,p0Denom = 2, 2\n",
    "    #遍历所有的doc向量\n",
    "    for i in range(n_doc):\n",
    "        if labels[i] == 1:\n",
    "            p1num += docs_v[i]\n",
    "            p1Denom += np.sum(docs_v[i])\n",
    "        elif labels[i] ==0:\n",
    "            p0num += docs_v[i]\n",
    "            p0Denom += np.sum(docs_v[i])\n",
    "    return np.log(p1num/p1Denom),np.log(p0num/p0Denom),np.sum(labels)/n_doc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1doc,p0doc,pA = train(docs_v,data['labels'],docList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['I', 'ate', 'buying', 'cute', 'dalmation', 'dog', 'flea', 'food', 'garbage', 'has', 'help', 'him', 'how', 'is', 'licks', 'love', 'maybe', 'mr', 'my', 'not', 'park', 'please', 'posting', 'promble', 'quit', 'so', 'steak', 'stop', 'stupid', 'take', 'to', 'worthless']\n"
     ]
    }
   ],
   "source": [
    "print(docList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-3.04452244, -3.04452244, -2.35137526, -3.04452244, -3.04452244,\n",
       "       -1.94591015, -3.04452244, -2.35137526, -2.35137526, -3.04452244,\n",
       "       -3.04452244, -2.35137526, -3.04452244, -3.04452244, -3.04452244,\n",
       "       -3.04452244, -2.35137526, -3.04452244, -3.04452244, -2.35137526,\n",
       "       -2.35137526, -3.04452244, -2.35137526, -3.04452244, -2.35137526,\n",
       "       -3.04452244, -3.04452244, -2.35137526, -1.65822808, -2.35137526,\n",
       "       -2.35137526, -1.94591015])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1doc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "函数功能：\n",
    "贝叶斯分类：\n",
    "输入：\n",
    "p1doc,p0doc,pA\n",
    "输出：\n",
    "1/0\n",
    "'''\n",
    "def classify(doc,p1doc,p0doc,pA):\n",
    "    doc_v = doc2V(doc,docList)\n",
    "    p1 = np.sum(doc_v * p1doc) + np.log(pA)\n",
    "    p0 = np.sum(doc_v * p0doc) + np.log(1-pA)\n",
    "    if p1 > p0:\n",
    "        return 1\n",
    "    else :\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classify('love my dalmation'.split(' '),p1doc,p0doc,pA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classify('stupid garbage'.split(' '),p1doc,p0doc,pA)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a6dc62afd8b03c17538a9dfce2fcb18f62cec380cc7b77050462a64b7e4e4814"
  },
  "kernelspec": {
   "display_name": "Python 3.8.0 32-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
